Skip to main content

Java continue statement

Banner java icon

๐Ÿš€ Java continue Statement: The Art of Skipping! ๐ŸŽญโ€‹

The continue statement in Java is like that one friend who skips all the boring parts of a conversation and jumps straight to the good stuff. ๐Ÿ˜† It helps us skip the current iteration of a loop and move on to the next oneโ€”no hard feelings!

๐ŸŽฏ What's the Deal with continue?โ€‹

The continue statement works inside for, while, or do-while loops and behaves almost like break, except it doesnโ€™t rudely terminate the loopโ€”it just skips the current iteration and moves to the next.

๐Ÿค” How Does It Work?โ€‹

  • In a for loop, continue jumps directly to the increment/decrement part.
  • In a while or do-while loop, continue sends control straight to the condition check.

Letโ€™s see continue in action:

int n = 10;
for (int i = 0; i < n; i++) {
if (i % 2 != 0) { // Skip odd numbers
continue;
}
System.out.print(i + " ");
}

๐Ÿ”น Output:

0 2 4 6 8

See? All the odd numbers just vanished! Magic? Nope. Just continue doing its thing. ๐Ÿช„


๐Ÿ”ฅ Syntax of continueโ€‹

The syntax is as simple as skipping leg day at the gym. ๐Ÿ’ช

while (testExpression) {
// Some statements
if (continue-condition)
continue;
// More statements (skipped if continue is hit)
}

When the continue-condition is met, Java skips the remaining code inside the loop and jumps straight to the next iteration.


๐ŸŽญ Types of continue Statementsโ€‹

Java offers two flavors of continue statements:

๐Ÿฉ 1. Unlabeled continueโ€‹

This is the basic form of continue. It simply skips the current iteration of the loop itโ€™s in.

for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println("Odd number: " + i);
}

๐Ÿ”น Output:

Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9

๐ŸŽฏ 2. Labeled continueโ€‹

Sometimes, you need more control. Labeled continue lets you skip an iteration in an outer loop instead of just the current one. Think of it as a VIP pass to the next level. ๐ŸŽŸ๏ธ

outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) {
continue outer; // Skip to the next iteration of outer loop
}
System.out.println("i = " + i + ", j = " + j);
}
}

๐Ÿ”น Output:

i = 0, j = 0
i = 1, j = 0
i = 2, j = 0

Whenever j == 1, it skips the rest of the inner loop and moves to the next iteration of the outer loop. Pretty neat, right? ๐Ÿ˜Ž


๐ŸŽฌ continue in Action: A Real-Life Exampleโ€‹

Letโ€™s use continue to print only odd numbers from 0 to 9:

for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println("The number is " + i);
}

๐Ÿ”น Output:

The number is 1
The number is 3
The number is 5
The number is 7
The number is 9

Now, what if we donโ€™t use continue? We can achieve the same result with an if statement:

for (int i = 0; i < 10; i++) {
if (i % 2 != 0) {
System.out.println("The number is " + i);
}
}

The result is the same, but heyโ€”sometimes continue just makes things more fun! ๐ŸŽข


โš ๏ธ A Word of Cautionโ€‹

While continue is super useful, donโ€™t overuse it! Too many continue and break statements can make your code hard to read. Sometimes, a simple if statement does the job just as well.


๐ŸŽ‰ Conclusionโ€‹

The continue statement is like a "skip this one" button for loops. Itโ€™s useful when you need to avoid certain iterations without stopping the entire loop. ๐Ÿš€

  • Use unlabeled continue to skip the current iteration.
  • Use labeled continue when working with nested loops.
  • Donโ€™t abuse itโ€”clean code is happy code! ๐Ÿ˜ƒ

Thatโ€™s all, folks! Keep coding and keep skipping the boring parts! ๐ŸŽˆ

Happy Learning! ๐Ÿš€๐Ÿ˜ƒ